#我下的GNU make兼容性与 Powershell不怎么好。无法直接通过SHELL = powershell切换环境
CC = gcc
BUILD_DIR = .\build
# 列出所有 .c 文件:(若生成路径包含空格需用""包围)
SRCS2 = $(wildcard sys/*.c)
SRCS = $(wildcard *.c) $(subst /,\,$(SRCS2))

# 根据SRCS生成 .o 文件列表:
# OBJS = $(addprefix $(BUILD_DIR)/, $(patsubst %.c, %.o, $(SRCS)))
OBJS = $(addprefix $(BUILD_DIR)\, $(patsubst %.c, %.o, $(SRCS)))
# 根据SRCS生成 .d 文件列表:
DEPS = $(patsubst %.o, %.d, $(OBJS))

TARGET = test.exe

# 默认目标:
$(TARGET): $(OBJS)
	$(CC) -o $@ $^

# xyz.d 的规则由 xyz.c 生成:Linux Version sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.tmp > $@
#下三条语句要写在一个powershell里面，否则会报错，猜测可能会并行执行导致同步问题(?)
define getDependencyRule
	-powershell -Command "rm -Force $@"; \
	powershell -Command "$(CC) -MM $^ | Out-File -FilePath $@.tmp -Encoding ASCII; \
	(Get-Content $@.tmp) -replace '$*.o:', '$*.o $@ : ' | Out-File -FilePath $@ -Encoding ASCII; \
	rm -Force $@.tmp"
endef
$(BUILD_DIR)\\%.d : %.c
	$(getDependencyRule)
$(BUILD_DIR)\\sys\\%.d : sys\\%.c
	$(getDependencyRule)
# 模式规则:
#%.o: %.c
COMPIEL_CMD = $(CC) -c -o $@ $<
$(BUILD_DIR)\\%.o  : %.c 
	$(COMPIEL_CMD)
$(BUILD_DIR)\\sys\\%.o : sys\\%.c
	$(COMPIEL_CMD)

clean:
	powershell -Command "rm -Recurse -Force *.o *.d $(TARGET)"
info:
	@echo '$(SRCS)'
	@echo '$(SRCS2)'
	@echo '$(OBJS)'
	@echo '$(DEPS)'
# 引入所有 .d 文件:
include $(DEPS)